package com.cloud.service.coupon.impl;

import com.cloud.bean.coupon.CouponRule;
import com.cloud.mapper.coupon.CouponRuleMapper;
import com.cloud.service.coupon.CouponRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuyuan
 * @since 2022-08-06
 */
@Service
public class CouponRuleServiceImpl extends ServiceImpl<CouponRuleMapper, CouponRule> implements CouponRuleService {
    @Autowired
    CouponRuleMapper couponRuleMapper;

    @Override
    public List<CouponRule> listByBatchCouponId(Long id) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("batch_coupon_id",id);
        return couponRuleMapper.selectByMap(queryMap);
    }

    @Override
    public CouponRule getByIdAndRuleId(Long batchCouponId, Long rule) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("batch_coupon_id",batchCouponId);
        queryMap.put("rule_id",rule);
        List<CouponRule> couponRules = couponRuleMapper.selectByMap(queryMap);
        if(couponRules.size() != 1){
            return null;
        }else {
            return couponRules.get(0);
        }
    }

    @Override
    public boolean delByRuleIdAndBatchCouponId(Long ruleId, Long id) {
        Map<String, Object> delMap = new HashMap<>();
        delMap.put("batch_coupon_id",id);
        delMap.put("rule_id",ruleId);
        int i = couponRuleMapper.deleteByMap(delMap);
        if(i == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int delByBatchCouponId(Long id) {
        Map<String, Object> delMap = new HashMap<>();
        delMap.put("batch_coupon_id",id);
        return couponRuleMapper.deleteByMap(delMap);
    }
}
